package logic

import (
	"context"
	"time"
	"xialou_platform/app/errorx"
	"xialou_platform/services/order/model"

	"xialou_platform/services/order/rpc/internal/svc"
	"xialou_platform/services/order/rpc/orderClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建订单
func (l *OrderAddLogic) OrderAdd(in *orderClient.OrderParam) (*orderClient.IdBody, error) {
	id := l.svcCtx.Flake.Generate().Int64()
	nowTime := time.Now()

	_, err := l.svcCtx.OrderModel.Insert(l.ctx, &model.Order{
		Id:         id,
		Type:       in.Type,
		SourceId:   in.SourceId,
		Money:      in.Money,
		Uid:        in.Uid,
		Status:     in.Status,
		OrderNum:   in.OrderNum,
		TradeType:  in.TradeType,
		PayType:    in.PayType,
		Remark:     in.Remark,
		CreateTime: nowTime,
		UpdateTime: nowTime,
		DelFlag:    false,
	})
	if err != nil {
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}

	return &orderClient.IdBody{
		Id: id,
	}, nil
}
